<template>
    <div class="search">
        <div class="list">
            <div class="item" v-for="(item, index) in search.list" @click="toDetails({ item })">
                <div class="caption">
                    <div class="name">{{ item.name }} ({{ item.type }})</div>
                    <div class="code">{{ item.code }}</div>
                </div>
            </div>
        </div>
        <div class="input">
            <input type="text" placeholder="请输入关键字...." v-model="search.word" />
        </div>
    </div>
</template>


<script setup>
import { reactive, computed, watch, nextTick, onMounted } from "vue"
import { useRouter } from "@/router"

import $api from "@/api"

let timer = null
const router = useRouter()

const search = reactive({
    word: "",
    list: ""
})

watch(() => search.word, (word) => {
    clearTimeout(timer)
    timer = setTimeout(async () => {
        let { data = [] } = await $api.stock.search({ word })
        search.list = data
    }, 1000)
})

const toDetails = ({ item }) => {
    let { code, type } = item
    router.push({ path: "/pages/index/details", query: { code, type } })
}

</script>


<style lang="scss" scoped>
.search {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background-color: #161922;

    >.list {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: flex-end;

        >.item {
            padding: 20upx;
            border-bottom: 1upx solid #000;
            color: #fff;

            >.caption {
                >.name {
                    font-weight: bold;
                }

                >.code {
                    margin-top: 6upx;
                    font-weight: bold;
                    font-size: 24upx;
                }
            }
        }
    }

    >.input {
        padding: 30upx 20upx;
        border-top: 1upx solid #000;
        text-align: center;

        input {
            width: 100%;
            box-sizing: border-box;
            color: #579dfd;
        }
    }
}
</style>